﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aptech.UI;
using Remind.BLL;
using Remind.Model;

namespace Remind.View
{
    public partial class frmAddGroup : Form
    {
        SideBar mysb;
        int gid = 0;
        GroupBLL group = new GroupBLL();

        public frmAddGroup()
        {
            InitializeComponent();
        }

        public frmAddGroup(SideBar sb)
        {
            InitializeComponent();
            mysb = sb;
        }

        public frmAddGroup(SideBar sb, GroupInfo group)
        {
            InitializeComponent();
            mysb = sb;
            gid=group.Gid;
            txtGroupName.Text = group.Gname;
            this.Text = "重命名";
            label1.Text = "新组名";
        }

        private void frmAddGroup_Load(object sender, EventArgs e)
        {

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.Text == "添加分组")
            {
                //判断
                if (txtGroupName.Text == "")
                {
                    MessageBox.Show("分组名称不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                //判断分组名称是否重复
                if (group.GetGroupByGname(txtGroupName.Text) != null)
                {
                    MessageBox.Show("组名已存在，请重新输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtGroupName.Text = "";
                    txtGroupName.Focus();
                    return;
                }
                if (group.AddGroup(txtGroupName.Text) > 0)
                {
                    mysb.AddGroup(txtGroupName.Text);
                }
                Close();
            }
            else
            {
                //判断
                if (txtGroupName.Text == "")
                {
                    MessageBox.Show("分组名称不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                //判断分组名称是否重复
                if (group.GetGroupByGname(txtGroupName.Text) != null)
                {
                    MessageBox.Show("组名已存在，请输入新组名！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtGroupName.Text = "";
                    txtGroupName.Focus();
                    return;
                }
                if(group.EditGroup(txtGroupName.Text,gid)>0)
                {
                    mysb.VisibleGroup.Text = txtGroupName.Text;
                }
                Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
